/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.fields;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.daikit.commons.shared.utils.DkObjectUtils;
import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.ui.forms.AbstractDkHideableAdapterField;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.form.ComboBox;


public class MyComboBox<T extends Serializable> extends AbstractDkHideableAdapterField<T>
{
	@Ignore
	private ComboBox<ComboObject<T>> comboBox;

	public MyComboBox(final List<T> choices)
	{
		this(choices, false);
	}

	public MyComboBox(final Map<T, String> choices)
	{
		this(choices, false);
	}

	public MyComboBox(final List<T> choices, final boolean addEmptyChoice)
	{
		super(null);
		preInitialize(ComboObject.getComboObjects(choices), addEmptyChoice);
	}

	public MyComboBox(final Map<T, String> choices, final boolean addEmptyChoice)
	{
		super(null);
		preInitialize(ComboObject.getComboObjects(choices), addEmptyChoice);
	}

	private void preInitialize(final List<ComboObject<T>> comboChoices, final boolean addEmptyChoice)
	{
		comboBox = new ComboBox<ComboObject<T>>(new ListStore<ComboObject<T>>(ComboObject.getProperties().key()), ComboObject
				.getProperties().string());
		comboBox.setEmptyText(DkMain.i18n().message_empty_combo_box_label());
		comboBox.setForceSelection(true);
		comboBox.setEditable(true);
		comboBox.setTypeAhead(true);
		comboBox.setTriggerAction(TriggerAction.ALL);
		initialize(comboChoices, addEmptyChoice);
		setWidget(comboBox);
	}

	protected void initialize(final List<ComboObject<T>> comboChoices, final boolean addEmptyChoice)
	{
		comboBox.getStore().clear();
		comboBox.getStore().addAll(comboChoices);
		if (addEmptyChoice)
		{
			comboBox.getStore().add(0, new ComboObject<T>(null, DkMain.i18n().message_empty_combo_box_label()));
		}
	}

	public ComboObject<T> getComboObject(final T object)
	{
		ComboObject<T> ret = null;
		for (final ComboObject<T> comboObject : comboBox.getStore().getAll())
		{
			if (DkObjectUtils.equalsWithNull(comboObject.getObject(), object))
			{
				ret = comboObject;
				break;
			}
		}
		return ret;
	}

	public int getComboObjectIndex(final T object)
	{
		int ret = -1;
		final List<ComboObject<T>> all = comboBox.getStore().getAll();
		for (int i = 0; i < all.size(); i++)
		{
			if (DkObjectUtils.equalsWithNull(all.get(i).getObject(), object))
			{
				ret = i;
				break;
			}
		}
		return ret;
	}

	@Override
	public T getValue()
	{
		return comboBox.getCurrentValue() == null ? null : comboBox.getCurrentValue().getObject();
	}

	@Override
	public void setValue(final T value)
	{
		comboBox.setValue(getComboObject(value));
	}

	public ComboBox<ComboObject<T>> getField()
	{
		return comboBox;
	}

	public void setChoices(final Map<T, String> choices, final boolean addEmptyChoice)
	{
		initialize(ComboObject.getComboObjects(choices), addEmptyChoice);
	}

	public void setChoices(final List<T> choices, final boolean addEmptyChoice)
	{
		initialize(ComboObject.getComboObjects(choices), addEmptyChoice);
	}
}
